package com.allen.service;

import com.allen.bean.Mywork;
import com.allen.bean.Result;
import com.allen.bean.Work;
import com.allen.mapper.MyworkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
@Transactional(rollbackFor = RuntimeException.class)
public class MyworkService {

    @Autowired
    private MyworkMapper myworkMapper;

    /**
     * 上传作品
     */
    public Result submit(Mywork mywork){
        Result result = new Result();       //实例化对象
        result.setSuccess(false);           //给变量赋值
        result.setDetail(null);
        myworkMapper.sub(mywork);
        System.out.println(mywork.getU_font());
        result.setMsg("发布成功");
        result.setSuccess(true);
        result.setDetail(mywork);

        return result;
    }

    /**
     * 查询作品
     */

    public List<Mywork> selWork(Mywork mywork){

        List<Mywork> list=myworkMapper.findWorksByName(mywork.getUsername());
        System.out.println(list);
        return list;
//        ArrayList arrayList=new ArrayList();
//        //拿到数据库查询的结果
//        ResultSet rs= (ResultSet) myworkMapper.findWorksByName(mywork.getUsername());
//        while(rs!=null){
//            System.out.println(rs);
////            long id= (long) rs.get(1);
//            String username= rs.getString(2);
//            String u_font= rs.getString(3);
//            String img_address= rs.getString(4);
//            System.out.println(username+""+u_font+""+img_address);
////            mywork.setId((Integer) rs.get(1));
//            mywork.setUsername(username);
//            mywork.setU_font(u_font);
//            mywork.setImg_address(img_address);
//            arrayList.add(mywork);          //将每一条数据添加到集合中
//        }
//
//        return arrayList;


    }

    /**
     * 查询所有用户的所有作品（即对应表中的全部数据）
     */
    public List<Mywork> selall(){

        List list=myworkMapper.findAll();

        return list;
    }

    /**
     * 增加浏览量的业务逻辑
     */
    public Result add(Work work){
        Result result = new Result();       //实例化对象
        result.setSuccess(false);           //给变量赋值
        result.setDetail(null);
        Integer res=myworkMapper.addeye(work);
        if(res==1){
            result.setMsg("浏览量+1");
            result.setSuccess(true);
        }else{
            result.setMsg("浏览失败");
            result.setSuccess(false);
        }
        return result;
    }


}
